package com.iap.common.service
{
	import com.iap.admin.event.VectorEvent;
	import com.iap.app.data.AppData;
	import com.iap.app.data.IAppData;
	import com.iap.app.model.ServiceResponse;
	import com.iap.common.model.VectorModel;
	import com.iap.common.translator.VectorTranslator;
	
	import org.robotlegs.mvcs.Actor;

	public class VectorService extends Actor implements IVectorService
	{
		[Inject]
		public var data:IAppData;
		
		private var _task:String;
		public function set task(value:String):void { _task = value; }

		public function getList():void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_GET);
		}
		
		public function save(model:VectorModel):void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_SAVE, model);
		}
		
		public function remove(model:VectorModel):void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, AppData.JOB_REMOVE, model);
		}

		public function getByTag(tagIds:Array):void
		{
			data.dataComplete.add(responseHandler);
			var urlTag:String = "tag/"+ tagIds.join(',');
			data.load(this, urlTag);
		}
		
		public function validRemove(model:VectorModel):void
		{
			data.dataComplete.add(responseHandler);
			data.load(this, "validRemove/"+ model.id);
		}

		private function responseHandler(response:ServiceResponse):void
		{
			data.dataComplete.remove(responseHandler);
			response.task = _task;
			response.result = VectorTranslator.translate(response.result);
			dispatch(new VectorEvent(VectorEvent.RESPONSE, response));
		}
	}
}